package com.ht.config.onlyoffcie;

import com.alibaba.fastjson.annotation.JSONField;
import com.ht.config.onlyoffcie.properties.OnlyProperties;
import com.ht.module.file.entity.FileInfo;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

/**
 * only office 配置信息
 * 以下参数必须填写
 * 1、docServiceApiUrl
 * 2、fileType
 * 3、key
 * 4、title
 * 5、url
 * 6、callbackUrl
 *
 * @author hejialun
 * @version 1.0
 */
@Data
public class FileConfigDTO {

    /**
     * 类型 embedded/desktop
     * 默认为desktop
     */
    private String type = "desktop";

    /**
     * 文档类型 text/spreadsheet/presentation
     * open a text document (.doc, .docm, .docx, .dot, .dotm, .dotx, .epub, .fodt, .htm, .html, .mht, .odt, .ott, .pdf, .rtf, .txt, .djvu, .xps)
     * open a spreadsheet (.csv, .fods, .ods, .ots, .xls, .xlsm, .xlsx, .xlt, .xltm, .xltx)
     * open a presentation (.fodp, .odp, .otp, .pot, .potm, .potx, .pps, .ppsm, .ppsx, .ppt, .pptm, .pptx)
     *
     */
    private DocumentType documentType;

    /**
     * 自定义签名
     */
    private String token;


    /**
     * 文档配置信息
     */
    @JSONField(name = "document")
    private FileDocument document;

    /**
     * 编辑配置
     */
    private EditorConfig editorConfig;

    /**
     * 访问api 路径
     */
    private String docServiceApiUrl;






    /**
     * @Author hejialun
     * @Date 9:24 2022/8/25
     * @Description TODO(初始化)
     * @param config：yml配置
     * @param fileInfo：文件实体
     * @return com.ht.onlyoffice.dto.FileConfigDTO
     */
    public FileConfigDTO init(OnlyProperties config, FileInfo fileInfo){
        //设置文件类型
        this.documentType=getDocumentType(fileInfo.getFilePath());
        //设置编辑配置
        this.editorConfig=new EditorConfig().init(config,fileInfo.getFilePath());
        //初始化文档配置信息
        this.document=new FileDocument().init(config,fileInfo);
        //设置访问api地址
        this.docServiceApiUrl=config.getDocService()+ OnlyOfficeConfig.DOC_API_URL;
        return this;
    }






    /**
     * @Author hejialun
     * @Date 15:08 2022/8/24
     * @Description TODO(获取文件类型)
     * @param url:文件路径
     * @return java.lang.String
     */
    private DocumentType getDocumentType(String url) {
        String suffix=url.substring(url.lastIndexOf("."));
        // 获取文件的扩展名 转换为小写
        String ext = suffix.toLowerCase();
        if (ExtsDocument.contains(ext)) {
            return DocumentType.Text;
        } else if (ExtsSpreadsheet.contains(ext)) {
            return DocumentType.Spreadsheet;
        } else if (ExtsPresentation.contains(ext)) {
            return DocumentType.Presentation;
        }
        return DocumentType.Spreadsheet;
    }


    private static List<String> ExtsDocument = Arrays.asList(
            ".doc", ".docx", ".docm",
            ".dot", ".dotx", ".dotm",
            ".odt", ".fodt", ".ott", ".rtf", ".txt",
            ".html", ".htm", ".mht",
            ".pdf", ".djvu", ".fb2", ".epub", ".xps");

    private static List<String> ExtsSpreadsheet = Arrays.asList(
            ".xls", ".xlsx", ".xlsm",
            ".xlt", ".xltx", ".xltm",
            ".ods", ".fods", ".ots", ".csv");

    private static List<String> ExtsPresentation = Arrays.asList(
            ".pps", ".ppsx", ".ppsm",
            ".ppt", ".pptx", ".pptm",
            ".pot", ".potx", ".potm",
            ".odp", ".fodp", ".otp");
}
